package com.wt.easyadmin.wx.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.wt.easyadmin.common.Constants;
import com.wt.easyadmin.common.annotation.JwtToken;
import com.wt.easyadmin.common.base.PageResultSet;
import com.wt.easyadmin.common.base.Result;
import com.wt.easyadmin.common.base.ResultCodeEnum;
import com.wt.easyadmin.common.utils.JwtUtil;
import com.wt.easyadmin.controller.BaseController;
import com.wt.easyadmin.model.bar.ProfitVo;
import com.wt.easyadmin.model.bar.TBarCashOut;
import com.wt.easyadmin.model.bar.TWxUser;
import com.wt.easyadmin.service.bussiness.CashOutService;
import com.wt.easyadmin.wx.WxUtils;
import com.wt.easyadmin.wx.entity.SettleInfo;
import com.wt.easyadmin.wx.entity.UserInfo;
import com.wt.easyadmin.wx.entity.WeiXinOAuthGetUserMobileReqDTO;
import com.wt.easyadmin.wx.service.WxService;

@RequestMapping("/wx")
@RestController
public class WxController extends BaseController {

	@Autowired
	private WxService wxService;

	@Autowired
	private CashOutService cashOutService;
	
	@RequestMapping("/session")
	public Result<UserInfo> getSession(@RequestParam(required = true) String code,String reference, HttpServletResponse httpServletResponse) {
		logger.info("getSession_code:{}",code,reference);
		UserInfo userInfo = wxService.getSessionByCode(code,reference);
	   	String token = JwtUtil.sign(userInfo.getOpenid());
    	httpServletResponse.setHeader(Constants.JWT_TOKEN_KEY, token);
		return Result.Success(userInfo);
	}
	
	@JwtToken
	@PostMapping(value = "/getUserMobile", consumes = "application/json", produces = "application/json")
	public Result<String> getUserMobile( @RequestBody @Valid WeiXinOAuthGetUserMobileReqDTO reqdto , HttpServletRequest req) {
	    try {
	    	String moblile = WxUtils.decrypt(reqdto.getEncryptedData(),reqdto.getCode(),reqdto.getIv());
	    	String phoneNumber = JSONObject.parseObject(moblile).getString("phoneNumber");
			String token = req.getHeader(Constants.JWT_TOKEN_KEY);
			String userId = JwtUtil.getUserId(token);//获取openid
			TWxUser tWxUser = new TWxUser(userId,phoneNumber);
			wxService.updateUser(tWxUser);
	        return Result.Success(moblile);
	    } catch (Exception e) {
	    	logger.error("getUserMobile error: ", e);
	        return Result.Failure(ResultCodeEnum.BadRequest);
	    }
	}
		 

	/**
	 * 获取提现信息
	 * @return
	 */
	@JwtToken
    @GetMapping("/getProfit")
    public Result<ProfitVo> getProfit() {
        return Result.Success(wxService.getProfit(WxUtils.getOpenid()));
    }
	
	/**
	 * 绑定手机号
	 * @param tWxUser
	 * @param req
	 * @return
	 */
	@JwtToken
	@PostMapping(value = "/updateUser", consumes = "application/json", produces = "application/json")
    public Result<String> updateUser(@RequestBody TWxUser tWxUser) {
		tWxUser.setOpenid(WxUtils.getOpenid());
		wxService.updateUser(tWxUser);
        return Result.Success();
    }
	@JwtToken
	@PostMapping("cashOutlist")
	public Result<PageResultSet<TBarCashOut>> cashOutlist(@RequestBody TBarCashOut tBarCashOut){
		logger.info("cashOutlist:"+JSONObject.toJSONString(tBarCashOut));
		return Result.Success(wxService.cashOutlistByUser(tBarCashOut));	
	}
	
	@JwtToken
	@PostMapping("settlelist")
	public Result<PageResultSet<SettleInfo>> settlelist(@RequestBody SettleInfo settleInfo){
		logger.info("settlelist:"+JSONObject.toJSONString(settleInfo));
		return Result.Success(wxService.settlelistByUserAndStatus(settleInfo));	
	}


}
